//
//  ElecItemView.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/11/17.
//

import Foundation
import UIKit
import Kingfisher

class ElecItemView: UIView {
    
    var key: String? {
        didSet {
            keyL.text = key
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withValue value: String?) {
        valueL.text = value
    }
    
    func setupUI() {
        let line = UIView()
        line.backgroundColor = .hex("#F3F3F3")
        addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(11)
            make.right.equalToSuperview().offset(-13)
            make.height.equalTo(0.5)
        }
        
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left).offset(13)
            make.width.equalTo(100)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        addSubview(valueL)
        valueL.snp.makeConstraints { make in
            make.left.equalTo(keyL.snp.right).offset(10)
            make.right.equalTo(self.snp.right).offset(-13)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(-16)
        }
    }
    
    lazy var keyL: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.textColor = .hex("#666666")
        lab.adjustsFontSizeToFitWidth = true
        return lab
    }()
    
    lazy var valueL: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 15)
        lab.textColor = .hex("#444444")
        lab.textAlignment = .right
        lab.numberOfLines = 0
        return lab
    }()
}

class ElecImgView: UIView {
    
    var key: String? {
        didSet {
            keyL.text = key
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updateUI(withImgURL imgURL: String? = nil) {
        if let imgURL = imgURL {
            let url = BaseURL + imgURL
            imgIV.kf.setImage(with: URL(string: url))
        }
    }
    
    func setupUI() {
        let line = UIView()
        line.backgroundColor = .hex("#F3F3F3")
        addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(11)
            make.right.equalToSuperview().offset(-13)
            make.height.equalTo(0.5)
        }
        
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left).offset(13)
            make.width.equalTo(150)
            make.centerY.equalToSuperview()
        }
        
        addSubview(imgIV)
        imgIV.snp.makeConstraints { make in
            make.right.equalTo(self.snp.right).offset(-13)
            make.width.equalTo(70)
            make.height.equalTo(56)
            make.top.bottom.equalToSuperview()
        }
    }
    
    lazy var keyL: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.textColor = .hex("#666666")
        lab.adjustsFontSizeToFitWidth = true
        return lab
    }()
    
    lazy var imgIV: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        return iv
    }()
}

class ElecImgTitleView: ElecImgView {
    
    func updateUI(withImgURL imgURL: String? = nil,  value: String?) {
        if let imgURL = imgURL {
            let url = BaseURL + imgURL
            imgIV.kf.setImage(with: URL(string: url))
        }
        
        valueL.text = value
    }
    
    override func setupUI() {
        super.setupUI()
        
        addSubview(valueL)
        valueL.snp.makeConstraints { make in
            make.right.equalTo(self.snp.right).offset(-13)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        imgIV.snp.remakeConstraints { make in
            make.right.equalTo(valueL.snp.left).offset(-4)
            make.width.equalTo(70)
            make.height.equalTo(56)
            make.top.bottom.equalToSuperview()
        }
    }
    
    lazy var valueL: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 15)
        lab.textColor = .hex("#444444")
        lab.textAlignment = .right
        lab.numberOfLines = 0
        return lab
    }()
}
